package com.firstcare.android_dylibrary.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.firstcare.android_dylibrary.DYApplication;
import com.firstcare.android_dylibrary.bean.base.BaseModelProxy;
import com.firstcare.android_dylibrary.network.URLSet;
import com.firstcare.android_dylibrary.network.request.DYRequest;
import com.rdxer.xxlibrary.HTTPUtils.listener.ErrorListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.FailedListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.OKListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenyichao on 2016/7/3.
 */
public class ProcessDiagramProxy extends BaseModelProxy<ProcessDiagram> {
    public static void getProcessDiagramGroups(final OKListener<List<List<ProcessDiagramProxy>>> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id","222");
        DYRequest<Object> request = new DYRequest<Object>(URLSet.getProjectExcel, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                String result = "{\n" +
                        "    \"errcode\": 0,\n" +
                        "    \"data\": {\n" +
                        "        \"groups\":[\n" +
                        "            [\n" +
                        "                {\n" +
                        "                    \"id\":1001,\n" +
                        "                    \"type\":1,\n" +
                        "                    \"state\":0,\n" +
                        "                    \"title\":\"直燃机1\",\n" +
                        "                    \"inV\":12,\n" +
                        "                    \"outV\":10,\n" +
                        "                    \"unit\":\"℃\"\n" +
                        "                },{\n" +
                        "                    \"id\":10012,\n" +
                        "                    \"type\":1,\n" +
                        "                    \"state\":1,\n" +
                        "                    \"title\":\"直燃机2\",\n" +
                        "                    \"inV\":12,\n" +
                        "                    \"outV\":60,\n" +
                        "                    \"unit\":\"℃\"\n" +
                        "                },{\n" +
                        "                    \"id\":10301,\n" +
                        "                    \"type\":1,\n" +
                        "                    \"state\":0,\n" +
                        "                    \"title\":\"直燃机3\",\n" +
                        "                    \"inV\":12,\n" +
                        "                    \"outV\":10,\n" +
                        "                    \"unit\":\"℃\"\n" +
                        "                },{\n" +
                        "                    \"id\":11012,\n" +
                        "                    \"type\":1,\n" +
                        "                    \"state\":1,\n" +
                        "                    \"title\":\"直燃机4\",\n" +
                        "                    \"inV\":12,\n" +
                        "                    \"outV\":60,\n" +
                        "                    \"unit\":\"℃\"\n" +
                        "                }\n" +
                        "            ],\n" +
                        "            [\n" +
                        "                {\n" +
                        "                    \"id\":10011,\n" +
                        "                    \"type\":2,\n" +
                        "                    \"state\":0,\n" +
                        "                    \"title\":\"循环泵1\",\n" +
                        "                    \"inV\":12,\n" +
                        "                    \"outV\":60,\n" +
                        "                    \"unit\":\"°\"\n" +
                        "                },{\n" +
                        "                    \"id\":10051,\n" +
                        "                    \"type\":2,\n" +
                        "                    \"state\":1,\n" +
                        "                    \"title\":\"循环泵2\",\n" +
                        "                    \"inV\":12,\n" +
                        "                    \"outV\":60,\n" +
                        "                    \"unit\":\"°\"\n" +
                        "                }\n" +
                        "            ]\n" +
                        "        ]\n" +
                        "    },\n" +
                        "    \"errmsg\": \"ok\"\n" +
                        "}\n";
                List<List<ProcessDiagramProxy>> list = new ArrayList<>();
                JSONArray array = JSON.parseObject(result).getJSONObject("data").getJSONArray("groups");
                for (int i = 0; i < array.size(); i++) {
                    JSONArray arrayM = array.getJSONArray(i);
                    if (arrayM.size() < 1){
                        continue;
                    }
                    list.add(generateModelProxyList(arrayM,ProcessDiagramProxy.class));
                }
                okListener.onOK(list,response);
            }
        },failedListener,errorListener){
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response.getJSONObject("data").getString("string");
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
}
